var app = getApp();
Page(Object.assign({},{
    data:{
        options : null,
        inited : false,
        checked : true,
        shopid : 0,
        info : {},
        currentstart : new Date().getTime(),
        currentend : new Date().getTime(),
        alladmin : [],
        allsort : [],
        showsort : false,
        params : {},
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        self.setData({
            options : options,
        });
        var id = options.id ? options.id : 0;
        app.com.auth(function(user){
            app.com.http('admin','POST',{op:'addlivestart',id:id},0,false,'','',function(res){
                if (!res.data.errno) {
                    self.setData({
                        info : res.data.data.info ? res.data.data.info : {list:[]},
                        shopid : res.data.data.shopid ? res.data.data.shopid : 0,
                        checked : res.data.data.info && res.data.data.info.ispub == 1 ? true : false,
                        alladmin : res.data.data.alladmin ? res.data.data.alladmin : [],
                        allsort : res.data.data.allsort ? res.data.data.allsort : [],
                        params : res.data.data.params ? res.data.data.params : [],
                        inited : true,
                    });

                    wx.setNavigationBarTitle({
                        title: '申请直播',
                    });

                }else{
                    app.com.alert( res.data.message,function(){
                        if( res.data.errno == 2 ){
                            app.globalData.adminurl = app.com.getUrlArgs();
                            wx.navigateTo({
                                url : res.data.data.url
                            });
                        }else{
                            wx.navigateBack();
                        }
                    });
                }
            });
        });
    },
    searchtel(){
        this.setData({
            isshowuser : true,
        })
    },
    pub(){
        var self = this;

        if( !self.data.params.xcxmess || !wx.requestSubscribeMessage ){
            self.savedata();
        }else{
            wx.requestSubscribeMessage({
                tmplIds : self.data.params.xcxmess,
                complete : function(){
                    self.savedata();
                }
            })
        }
    },
    savedata(){
        var self = this;

        self.data.info.ispub = self.data.checked ? 1 : 0;

        var form = JSON.stringify( self.data.info );
        app.com.http('admin','POST',{op:'publive',form:form},0,true,'','',function(res){
            if (!res.data.errno) {
                app.com.toast(res.data.message,'success',function(){
                    wx.navigateBack();
                });
            }else{
                app.com.toast( res.data.message,'none' );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        });
    },
    afterRead(e){
        var self = this;
        wx.showLoading({mask:true,title:'上传中'});
        wx.uploadFile({
            url: app.siteInfo.siteroot+'?i='+app.siteInfo.uniacid+'&j=&c=utility&a=file&do=upload&type=image&thumb=0',
            filePath: e.detail.file.path,
            name: 'file',
            success: function(res){
                var aaa = JSON.parse( res.data );

                if(e.currentTarget.dataset.type*1 == 2){
                    self.setData({
                        'info.bg' : aaa.attachment,
                        'info.showbg' : aaa.url
                    })
                }else if(e.currentTarget.dataset.type*1 == 3){
                    self.setData({
                        'info.headimg' : aaa.attachment,
                        'info.showheadimg' : aaa.url
                    })    
                }else{
                    self.setData({
                        'info.thumb' : aaa.attachment,
                        'info.showthumb' : aaa.url
                    })
                }
            },
            complete : function(){
                wx.hideLoading();
            },
            fail : function(rr){
                console.log(rr)
            }
        })
    },
    onChange(e){
        var keys = e.currentTarget.dataset.type;
        var key = 'info.'+keys;
        this.setData({
            [key] : e.detail
        })
    },
    changestatus(){
        this.setData({
            checked : !this.data.checked
        })
    },
    confirmstart(e){
        
        var starttime = app.com.totime(e.detail);
        this.setData({
            'info.starttime' : starttime,
        })
        this.onClose();
    },
    confirmend(e){
        var starttime = app.com.totime(e.detail);
        this.setData({
            'info.endtime' : starttime,
        })
        this.onClose();
    },
    delimg(e){
        this.setData({
            'info.img' : '',
            'info.showimg' : '',
        })
    },
    selectuser(e){
        var item = this.data.alladmin[e.currentTarget.dataset.i];
        this.setData({
            'info.uid' : item.uid,
            'info.nickname' : item.nickname,
            'info.showheadimg' : item.showheadimg,
            'info.headimg' : item.headimg,
        })
        this.onClose();
    },
    onClose(){
        this.setData({
            showstart : false,
            showend : false,
            isshowuser : false,
            showsort : false,
        })
    },
    showstartfn(e){
        if(e.currentTarget.dataset.type == 1){
            this.setData({
                showstart : true,
            })
        }else if(e.currentTarget.dataset.type == 3){
            this.setData({
                showsort : true,
            })    
        }else{
            this.setData({
                showend : true,
            })
        }
    },
    onConfirmsort(e){
        this.setData({
            'info.sortname' : this.data.allsort[e.detail.index].name,
            'info.sortid' : this.data.allsort[e.detail.index].id,
        })
        this.onClose();
    },
    selectgoods(e){

        if(!this.data.info.list){
            this.data.info.list = [];
        }

        this.data.info.list.push({
            title : e.detail.title ? e.detail.title : '',
            id : e.detail.id,
            url : e.detail.url,
            hxtimes :1,
        })
        this.setData({
            'info.list' : this.data.info.list
        })
    },
    showgoodsfn(){
        this.selectComponent("#goodslist").showgoodsfn(this.data.shopid);
    },
    delgitem(e){
        var i = e.currentTarget.dataset.i;
        this.data.info.list.splice(i,1);
        this.setData({
            'info.list' : this.data.info.list
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    
}))